layui.use(['table', 'admin', 'form', 'ax', 'upload'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var upload = layui.upload;
    /**
     * 门店装修表管理
     */
    var Mdzx = {
        tableId: "mdzxTable"
    };

    /**
     * 初始化表格的列
     */
    Mdzx.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {type: 'numbers', title: '序号', align: "center", width: 50},
            {field: 'btyZxMdzxId', hide: true, title: '主键'},
            // {field: 'btyBrandId', sort: true, title: '门店id'},
            {field: 'btyBrandIdView', align: "center", title: '门店名称', width: 300},
            // {field: 'mdBianma', align: "center", title: '门店编码'},
            // {field: 'mdZhujima', title: '门店助记码', align: "center",},
            // {field: 'btyArchivesIdView', title: '负责人姓名', align: "center",},
            // {field: 'fzrDianhua', title: '联系电话', align: "center",},
            {field: 'mianji', title: '总面积(平米)', width: 120, align: "center",},
            {
                field: 'yusuanHeji',
                title: '预算总计',
                totalRow: true,
                align: "center",
                width: 100,
                templet: function (row) {
                    if (row.yusuanHeji == null || row.yusuanHeji == '') {
                        return "0";
                    } else {
                        return row.yusuanHeji;
                    }
                }
            },
            {field: 'yujiTouzi', title: '预计投资Y',templet: '#yujiTouziTpl', totalRow: true, align: "center", width: 100},
            {
                field: 'chengbenHeji',
                title: '成本总计C',
                totalRow: true,
                align: "center",
                width: 100,
                templet: function (row) {
                    if (row.chengbenHeji == null || row.chengbenHeji == '') {
                        return "0";
                    } else {
                        return row.chengbenHeji;
                    }
                }
            },
            {
                field: 'touzishengyu',
                title: '投资剩余T=Y-C',
                totalRow: true,
                align: "center",
                width: 130,
                templet: function (row) {
                    if (row.touzishengyu == null || row.touzishengyu == '') {
                        return "0";
                    } else {
                        return row.touzishengyu;
                    }
                }
            },
            {field: 'createUserName', title: '创建人', width: 100, align: "center",},
            {field: 'createTime', title: '创建时间', align: "center",width: 170},
            // {field: 'updateUserName', sort: true, title: '修改人'},
            // {field: 'updateTime', sort: true, title: '修改时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 280}
        ]];
    };

    /**
     * 监听复选框选择
     */
    // table.on('checkbox(mdzxTable)', function(obj){
    //     window.location.href = Feng.ctxPath + '/zxChengben';
    // });


    /**
     * 点击查询按钮
     */
    Mdzx.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(Mdzx.tableId, {where: queryData});
    };

    /**
     * 弹出添加对话框
     */
    Mdzx.openAddDlg = function () {
        // window.location.href = Feng.ctxPath + '/dgdmdzx/add';
        parent.layer.open({
            type: 2,
            title: '新增门店',
            area: ['800px', '600px'],
            content: Feng.ctxPath + '/dgdmdzx/add',
            end: function () {
                Mdzx.search();
            },
            cancel:function (index){
                Mdzx.search();
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Mdzx.exportExcel = function () {
        var checkRows = table.checkStatus(Mdzx.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Mdzx.openEditDlg = function (data) {
        // window.location.href = Feng.ctxPath + '/dgdmdzx/edit?btyZxMdzxId=' + data.btyZxMdzxId;

        parent.layer.open({
            type: 2,
            title: '修改门店',
            area: ['800px', '600px'],
            content: Feng.ctxPath + '/dgdmdzx/edit?btyZxMdzxId=' + data.btyZxMdzxId,
            end: function () {
                Mdzx.search();
            },
            cancel:function (index){
                Mdzx.search();
            }
        });
    };

    /**
     * 复制
     *
     * @param data 点击按钮时候的行数据
     */
    Mdzx.openCopyDlg = function (data) {
        // window.location.href = Feng.ctxPath + '/dgdmdzx/copy?btyZxMdzxId=' + data.btyZxMdzxId;

        parent.layer.open({
            type: 2,
            title: '复制门店',
            area: ['800px', '600px'],
            content: Feng.ctxPath + '/dgdmdzx/copy?btyZxMdzxId=' + data.btyZxMdzxId,
            end: function () {
                Mdzx.search();
            },
            cancel:function (index){
                Mdzx.search();
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Mdzx.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/dgdmdzx/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                    table.reload(Mdzx.tableId);
                } else {
                    Feng.error(data.message);
                }
            });
            ajax.set("btyZxMdzxId", data.btyZxMdzxId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Mdzx.tableId,
        url: Feng.ctxPath + '/dgdmdzx/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        limit:20,
        totalRow: true,
        cols: Mdzx.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Mdzx.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Mdzx.openAddDlg();
    });

    //门店装修复制-与投资预算的复制一样
    $('#btnCopy').click(function () {
        var data = table.checkStatus(Mdzx.tableId);
        if (data.data.length == 0) {
            Feng.error("请选择一个门店");
            return false;
        } else if (data.data.length > 1) {
            Feng.error("只能选择一个门店");
            return false;
        }
        parent.layer.open({
            type: 2,
            title: '复制门店',
            area: ['800px', '600px'],
            content: Feng.ctxPath + '/dgdmdzx/copy?btyZxMdzxId=' + data.data[0].btyZxMdzxId,
            end: function () {
                Mdzx.search();
                // Feng.success("复制成功！");
            },
            cancel:function (index){
                // Mdzx.search();
            }
        });
    });

    // 导出excel
    $('#btnExp').click(function () {
        Mdzx.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Mdzx.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Mdzx.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Mdzx.onDeleteItem(data);
        } else if (layEvent === 'investmentSituation') {
            Mdzx.costSetting(data);
        }else if(layEvent==='viewYuanSuan'){
            Mdzx.viewYuanSuan(data);
        }
    });

    /**
     * 查看预算
     */
    Mdzx.viewYuanSuan = function (data) {
        window.location.href = Feng.ctxPath + '/zxYusuan/viewYuanSuan?btyZxMdzxId=' + data.btyZxMdzxId;
    };

    /**
     * 投资情况显示
     */
    Mdzx.costSetting = function (data) {
        window.location.href = Feng.ctxPath + '/dgdmdzx/tzqk2?btyZxMdzxId=' + data.btyZxMdzxId;
    };

    /**
     * 初始化门店
     */
    var languageAjax = new $ax(Feng.ctxPath + "/dgdmdzx/list");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        var pinyin = ConvertPinyin(val.btyBrandIdView);
        //查询需要选择的门店
        $("#btyZxMdzxId").append("<option value='" + val.btyZxMdzxId + "'>" + val.btyBrandIdView + "-" + pinyin + "</option>");
    });
    form.render('select');

    // 导出excel
    $('#btnExp').click(function () {
        Mdzx.exportExcel();
    });

    //投资情况导入
    var loadIndex;
    upload.render({
        elem: '#btnExcel'
        , url: '/zxTzqk/import'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            //this.data = {'dataTime': $('#dataTime').val()};//关键代码
            loadIndex = layer.load(2, {shade: [0.15, '#ccc']});
        }
        , done: function (data) {
            if (data.code == 200) {
                Feng.success("信息导入成功");
                // window.location.reload();
                layer.close(loadIndex);
            } else {
                Feng.error(data.message);
                layer.close(loadIndex);
            }

        }
    });

    //投资情况导入
    var loadIndex_md_tzqk;
    upload.render({
        elem: '#btnExcel_md_tzqk'
        , url: '/zxTzqk/import_md_tzqk'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            //this.data = {'dataTime': $('#dataTime').val()};//关键代码
            loadIndex_md_tzqk = layer.load(2, {shade: [0.15, '#ccc']});
        }
        , done: function (data) {
            if (data.code == 200) {
                Feng.success("信息导入成功");
                // window.location.reload();
                layer.close(loadIndex_md_tzqk);
            } else {
                Feng.error(data.message);
                layer.close(loadIndex_md_tzqk);
            }

        }
    });
    //门店投资 成本导入
    var btnMdTzCbLoading;
    upload.render({
        elem: '#btnMdTzCb'
        , url: '/zxTzqk/btnImportMdTzCb'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx'
        , before: function () {
            btnMdTzCbLoading = layer.load(2, {shade: [0.15, '#ccc']});
        }
        , done: function (data) {
            if (data.code == 200) {
                Feng.success("导入成功");
                layer.close(btnMdTzCbLoading);
            } else {
                Feng.error(data.message);
                layer.close(btnMdTzCbLoading);
            }

        }
    });

    var l_index;
    $("#batchbtnMdTzys").click(function () {
        $.ajax({
            url: Feng.ctxPath + "/zxTzqk/batchImportMdTzys",
            cache: false,
            dataType:"json",
            data: null,
            timeout: 600*1000,//10min
            beforeSend: function () {
                l_index = layer.load(1);
            },
            success: function (data) {
                if (data.code != 200) {
                    Feng.error(data.message);
                }else {
                    Feng.success("操作成功！");
                }
                layer.close(l_index);
            },
            error: function (data) {
                layer.alert('操作失败！');
                layer.close(l_index);
            }
        });
    });

    //监听行单击事件（双击事件为：rowDouble）
    table.on('row(mdzxTable)', function(obj){
        var data = obj.data;

        $('div[lay-id="mdzxTable"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样

        $('div[lay-id="mdzxTable"]').find(obj.tr.selector).attr({ "style": "background:#C0C0C0" });//改变当前tr颜色
        var btyZxMdzxId = data.btyZxMdzxId;

    });

});
